//
//  OptionalStudyViewController.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/5/2.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "OptionalStudyViewController.h"
#import "LoginViewController.h"
#import "ZJOptionalStudyModel.h"
#import "ZJUserInfoModel.h"
#import "ZJStudyBookTableViewCell.h"
//#import "LPSearchViewController.h"
//视频观看
#import "StudyCoureVideoViewController.h"

/** 视频、音频cell */
#import "ZJStudyVideoTableViewCell.h"
/** 图书cell */
#import "ZJStudyBookTableViewCell.h"
#define videoCell @"ZJStudyVideoTableViewCell"
#define bookCell  @"ZJStudyBookTableViewCell"
@interface OptionalStudyViewController ()<UITableViewDelegate,UITableViewDataSource>/** tableView */
@property (nonatomic, strong) UITableView *tableView;
/** dataArray */
@property (nonatomic, strong) NSMutableArray *dataArray;
/**  分页  */
@property (assign, nonatomic) NSInteger  page;
/** 下滑上滑控价 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
@end

@implementation OptionalStudyViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.view.backgroundColor = [UIColor viewBGGrayColor];
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
        
    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
//        [weakSelf netWorking_footer];
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    
    [self.tableView.mj_header  beginRefreshing];
    [self setupUI];
    
}

- (void)setupUI{
    [self.view addSubview:self.tableView];
    self.rightButton.hidden = NO;
   
}


- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{@"USER_ID":[ZJUserInfoModel sharedUserInfo].CURRENT_USER_MODEL.ID,
                           @"CATEGORY":@"0",
                           @"PAGE_NUMBER":@"1",
                           @"SHOW_NUMBER":@"10"};
    [NetWorkingManager postWithUrlString:[NetWorkingURL listCollection] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//请求成功
            weakSelf.dataArray = [[NSArray yy_modelArrayWithClass:[ZJOptionalStudyModel class] json:object[@"COLLECTIONS"]] mutableCopy];
            [weakSelf.tableView reloadData];
            if (weakSelf.dataArray.count == 0) {
                [[LPShowNoDataManager showNoDataManager] showEmptyViewWithView:weakSelf.tableView Type:1];
            }
        }
        [weakSelf.tableView.mj_header endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
        if (weakSelf.dataArray.count == 0) {
            [[LPShowNoDataManager showNoDataManager] showEmptyViewWithView:weakSelf.tableView Type:0];
            [LPShowNoDataManager showNoDataManager].noContentViewTapedBlock = ^{
                [weakSelf.tableView.mj_header beginRefreshing];
            };
        }else{
            [YJProgressHUD showSuccess:@"加载数据失败" inview:weakSelf.view];
        }
    }];
}

- (void)action_leftButton:(UIButton *)sender{
  
    [[ToolClass getMainNavigationController] popViewControllerAnimated:YES ];
}
- (void)action_rightButton:(UIButton *)sender{
//    LPSearchViewController *vc = [LPSearchViewController new];
//    vc.number = 0;
//    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - UITableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJOptionalStudyModel * model = self.dataArray[indexPath.row];
    if ([model.COURSETYPE isEqualToString:@"4"]) {//悦读会
        ZJStudyBookTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:bookCell forIndexPath:indexPath];
        cell.collectModel = model;
        return cell;
    }else{
        
        ZJStudyVideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:videoCell forIndexPath:indexPath];
        cell.collectModel = model;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJStudyRecommendModel *model = self.dataArray[indexPath.row];
    if ([model.COURSETYPE isEqualToString:@"4"]){//悦读会
        return 125 * [UIScreen zj_screenHScale];//图书
    }
    return 120 * [UIScreen zj_screenHScale];//视频、音频
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    StudyCoureVideoViewController *vc = [StudyCoureVideoViewController new];
    ZJOptionalStudyModel * cellModel = self.dataArray[indexPath.row];
    ZJStudySpacialDetailModel *model = [ZJStudySpacialDetailModel new];
    model.ID = cellModel.CSR_CLASS_ID;
    model.TEACHERVIDEO = cellModel.TEACHERVIDEO;
    model.NAME  =  cellModel.NAME;
    model.CLICKRATE = cellModel.CLICKRATE;
    vc.model = model;
    [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
}



#pragma mark - Setter


#pragma mark - Getters
- (NSMutableArray *)dataArray{
    if (_dataArray == nil){
        _dataArray = [NSMutableArray array];
        
    }
    return _dataArray;
}
- (UITableView *)tableView{
    if (_tableView == nil){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, navHeight, self.view.width, self.view.height - TabbarHeight - navHeight) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
     
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 7.5 * [UIScreen zj_screenHScale])];
        view.backgroundColor = [UIColor viewBGGrayColor];
        _tableView.tableHeaderView = view;
        [_tableView  registerClass:[ZJStudyVideoTableViewCell class] forCellReuseIdentifier: videoCell];
        [_tableView  registerClass:[ZJStudyBookTableViewCell class] forCellReuseIdentifier: bookCell];
    }
    return _tableView;
}



- (ZJRefurbish *)refurbish{
    if (_refurbish == nil){
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}

@end
